/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package filtros;

import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.awt.image.renderable.ParameterBlock;
import java.util.Vector;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.media.jai.TiledImage;

/**
 *
 * @author Igor
 */
public class Filtro {

//Filtro de mediana para imagens binárias
    public static PlanarImage filtroMediana (PlanarImage pi) {
        int width = pi.getWidth();
        int height = pi.getHeight();
        int count;

        Raster inputRaster = pi.getData();
        WritableRaster outputRaster = inputRaster.createCompatibleWritableRaster();
        int[] pixel = new int[3];

            for(int i =1; i < width-1; i++)
            {
                for (int h=1;h<height-1;h++) {
                    count=0;
                    for (int z=-1;z<2;z++) {
                        for (int k=-1;k<2;k++) {
                            pixel = inputRaster.getPixel(i+z, h+k, pixel);
                            if (pixel[0]==0) {
                                count++;
                            }
                        }
                    }
                    if (count>=5) {
                        pixel[0] = 0;
                        pixel[1]= 0;
                        pixel[2]= 0;
                    } else {
                        pixel[0] =255;
                        pixel[1] =255;
                        pixel[2] =255;
                    }
                    outputRaster.setPixel(i, h, pixel);
                }
            }

            TiledImage ti = new TiledImage(pi, width, height);
            ti.setData(outputRaster);
            return ti;
    }

    

}
